﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;

using System.Text;
using mmoss.configuration;
using mmoss.orm;
using mmoss.application.classes;
using System.Windows.Forms;

namespace mmoss.application
{
    
    public partial class frmAdminInventoryItemVendor : Form
    {

        public InventoryItemType ItemType { get; set; }
        private List<InventoryItem> AvailableItems { get; set; }
        private List<InventoryItem> SelectedItems { get; set; }
        private List<InventoryItemByVendorMMC> LocationItems { get; set; }
        public frmAdminInventoryItemVendor()
        {
            this.Load += new EventHandler(frmAdminInventoryItemVendor_Load);
            InitializeComponent();
        }

        void frmAdminInventoryItemVendor_Load(object sender, EventArgs e)
        {

            //gridview
            //grid properties
            dgItemVendor.AutoGenerateColumns = false;


            //bindgrid
            //dgItemVendor.Columns.Clear();
            DataGridViewComboBoxColumn l_col = ((DataGridViewComboBoxColumn)dgItemVendor.Columns["colItemName"]);
            l_col.ValueMember = "Id";
            l_col.DisplayMember = "itemShortDesc";
            l_col.DataSource = dbConn.db.InventoryItems.ToList();

            l_col = ((DataGridViewComboBoxColumn)dgItemVendor.Columns["colVendor"]);
            l_col.ValueMember = "vendorID";
            l_col.DisplayMember = "vendorName";
            l_col.DataSource = dbConn.db.Vendors.ToList();

            l_col = ((DataGridViewComboBoxColumn)dgItemVendor.Columns["colCostTable"]);
            l_col.ValueMember = "priceTableID";
            l_col.DisplayMember = "priceTableName";
            l_col.DataSource = dbConn.db.InventoryPriceTables;

            //events
            cboItemCategory.DataSource = dbConn.db.InventoryItemTypes;
            cboItemCategory.DisplayMember = "InventoryTypeDesc";
            cboItemCategory.ValueMember = "InventoryTypeID";
            cboItemCategory.SelectedIndexChanged += new EventHandler(cboItemCategory_SelectedIndexChanged);

            //find initial items in select box
            bindListBoxesInit();

            //set initial variables
            if (ItemType != null)
            {
                if (cboItemCategory.FindStringExact(ItemType.InventoryTypeDesc) != -1)
                {
                    cboItemCategory.SelectedIndex = cboItemCategory.FindStringExact(ItemType.InventoryTypeDesc);
                }
            }

            //vendors
            cboVendor.DisplayMember = "vendorName";
            cboVendor.ValueMember = "vendorID";
            cboVendor.DataSource = dbConn.db.Vendors.ToList();
            
            bindListBoxes();
            //bind grid
            loadGrid();
        }
        private void loadGrid()
        {
            //bind items
            try
            {
                LocationItems = Default.CurrentLocation.InventoryItemByVendorMMCS.Where(p => p.InventoryItem.InventoryItemType
                    == ((InventoryItemType)cboItemCategory.SelectedItem)).ToList();
                dgItemVendor.DataSource = LocationItems;
            }
            catch (Exception ex)
            {
                MessageBox.Show("error=" + ex.Message);
            }

        }
        void cboItemCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            //new category
            //change available list to add
            //change known item grid
            loadGrid();
            bindListBoxesInit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            foreach (InventoryItem l_item in SelectedItems)
            {
                //add this along w/vendor to the grid
                InventoryItemByVendorMMC l_newItem = new InventoryItemByVendorMMC();
                l_newItem.Location = Default.CurrentLocation;
                l_newItem.User = Default.CurrentUser;
                l_newItem.InventoryItem = l_item;
                l_newItem.Vendor = ((Vendor)cboVendor.SelectedItem);
                l_newItem.isActive = true;
            }
            dbConn.db.SubmitChanges();

            //reload the grid
            loadGrid();

        }

        private void btnMoveToSelected_Click(object sender, EventArgs e)
        {
            foreach (InventoryItem l_item in lbAvailableItems.SelectedItems)
            {
                AvailableItems.Remove(l_item);
                SelectedItems.Add(l_item);
            }
            bindListBoxes();
            
        }
        private void bindListBoxes()
        {

            lbAvailableItems.DisplayMember = "itemShortDesc";
            lbAvailableItems.ValueMember = "Id";
            BindingSource l_available = new BindingSource(AvailableItems,null);
            lbAvailableItems.DataSource = l_available;

            lbSelectedItems.DisplayMember = "itemShortDesc";
            lbSelectedItems.ValueMember = "Id";
            BindingSource l_selected = new BindingSource(SelectedItems,null);
            lbSelectedItems.DataSource = l_selected;
        }

        private void bindListBoxesInit()
        {
            //get items in location already in list
            SelectedItems = Default.CurrentLocation.InventoryItemByVendorMMCS.Select(p => p.InventoryItem).ToList();
            //get available items of selected type not already in list
            AvailableItems = dbConn.db.InventoryItems.Where(p => p.InventoryItemType == ((InventoryItemType)cboItemCategory.SelectedItem)).OrderBy(p => p.itemShortDesc).ToList();

            //bind data set
            bindListBoxes();
        }
        private void btnMoveFromSelected_Click(object sender, EventArgs e)
        {
            foreach (InventoryItem l_item in lbAvailableItems.SelectedItems)
            {
                SelectedItems.Remove(l_item);
                AvailableItems.Add(l_item);
            }
            bindListBoxes();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            dbConn.db.SubmitChanges();
            MessageBox.Show("Changes Updated");
            this.Close();
        }

    }
}
